﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Service
{
    /// <summary>
    /// 視聴ユーザ連想配列
    /// </summary>
    public sealed class NicoUserDictionary:Dictionary<string,NotificationNicoUser>
    {
        /// <summary>
        /// 渡したkeyが有効でなければkey情報をもとにオブジェクトを追加
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public NotificationNicoUser CreateIfNotExsits(string key)
        {
            if (!this.ContainsKey(key))
            {
                this.Add(key, new NotificationNicoUser(new SerializableNicoEncounterUser(key)));
            }

            return this[key];
        }


        /// <summary>
        /// 視聴ユーザリストをもとにユーザ情報を設定
        /// </summary>
        /// <param name="userList"></param>
        public void Set(ICollection<SerializableNicoEncounterUser> userList)
        {
            var expireDay = TimeSpan.FromDays(7);
            foreach (var userInfo in userList)
            {
                if ((!userInfo.IsAnonymous) || DateTime.Now - userInfo.RegistrationDate < expireDay)
                {
                    var notifyInfo = new NotificationNicoUser(userInfo);

                    this[notifyInfo.Id] = notifyInfo;
                }

            }
        }


        /// <summary>
        /// ユーザ情報を保存時しやすい形に変更
        /// </summary>
        /// <returns></returns>
        public ICollection<SerializableNicoEncounterUser> AsSerialize()
        {
            var userList = new List<SerializableNicoEncounterUser>();
            foreach (var user in this)
            {

                
                 userList.Add(user.Value.User);
                
            }

            return userList;
        }
    }
}
